package com.abigdreamer.im.server.ui.desktop.internal;

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import com.abigdreamer.im.server.module.EIMChatSocket;
import com.abigdreamer.im.server.module.EIMFileSocket;
import com.abigdreamer.im.server.module.EIMServer;
import com.abigdreamer.im.server.ui.resourse.ResourseMannager;

/**
 * 
 * 
 */
public class MyServerFrame extends DeskTopFrame{
	private MyServerWindow myServerWindow;
	public MyServerFrame(){
		super("服务器物理信息", false);
		initComponents();
		this.setClosable(true);
		this.setBounds(100, 40, 500, 500);
	}
	
	 private void initComponents() {
		    myServerWindow =new MyServerWindow();
		    this.getContentPane().add(myServerWindow,BorderLayout.CENTER);
	    }
}

/**
 * 
 * @author 胡海亮  QQ:249782944
 * 学校:中南大学 商学院
 * EnterpriseIMServer :企业即时通讯
 * May 4, 2010
 */
class MyServerWindow extends JPanel{
	
    public MyServerWindow(){
    	Box box=Box.createVerticalBox();
    	ServerSocketJPanel socketJPanel=new ServerSocketJPanel();
    	box.add(socketJPanel);
    	ServerDiskJPanel diskJPanel=new ServerDiskJPanel();
    	box.add(diskJPanel);
    	this.add(box, BorderLayout.CENTER);
    }
}

/**
 * 
 * @author 胡海亮  QQ:249782944
 * 学校:中南大学 商学院
 * EnterpriseIMServer :企业即时通讯
 * May 4, 2010
 * 
 * 服务器sockect信息
 */
class ServerSocketJPanel extends JPanel{
	
	public ServerSocketJPanel(){
		this.setBorder(BorderFactory.createTitledBorder("服务器连接信息"));
		Box box=Box.createVerticalBox();
		
		EIMServer server=EIMServer.getInstance();	
		
		EIMChatSocket chatSocket=server.getChatSocket();
		SocketJPanel chatSocketJPanel=new SocketJPanel("聊天服务",chatSocket.isStart());
		box.add(chatSocketJPanel);
		
		EIMFileSocket fileSocket=server.getFileSocket();
		SocketJPanel fileSocketJPanel=new SocketJPanel("文件服务",fileSocket.isStart());
		box.add(fileSocketJPanel);
		
		SocketJPanel noticeSocketJPanel=new SocketJPanel("广播服务",false);
		box.add(noticeSocketJPanel);
		
		this.add(box,BorderLayout.CENTER);
	}
	
	/**
	 * 
	 */
	class SocketJPanel extends JPanel{
		public SocketJPanel(String title,boolean isStart){
			Box box=Box.createHorizontalBox();
			JLabel lableIcon=new JLabel();
			Icon icon=ResourseMannager.getImageIcon("Signal.png", 32, 32);
			lableIcon.setIcon(icon);
			String istart="未开启";
			if(isStart){
				istart="已经开启";
			}
			lableIcon.setText(title);
			box.add(lableIcon);
			box.add(new JLabel("        "));
			box.add(new JLabel("        "));
			box.add(new JLabel(istart));
			box.add(new JLabel("        "));
			box.add(new JButton("开启?"));
			box.add(new JLabel("        "));
			box.add(new JButton("刷新?"));
			box.add(new JLabel("        "));
			box.add(new JButton("关闭?"));
			this.add(box,BorderLayout.CENTER);
		}
		
	}
	
}

/**
 * 
 * @author 胡海亮  QQ:249782944
 * 学校:中南大学 商学院
 * EnterpriseIMServer :企业即时通讯
 * May 4, 2010
 * 
 * 服务器磁盘信息
 */
class ServerDiskJPanel extends JPanel{
	public ServerDiskJPanel(){
		this.setBorder(BorderFactory.createTitledBorder("服务器磁盘信息"));
		Box box=Box.createVerticalBox();
		File[] roots = File.listRoots();// 获取磁盘分区列表
		for (File file : roots) {
			DiskJPanel diskJPanel=new DiskJPanel(file);
			box.add(diskJPanel);
		}
		this.add(box,BorderLayout.CENTER);
	}
	
	/**
	 * 
	 */
	class DiskJPanel extends JPanel{
		
		public DiskJPanel(File file){
			Box box=Box.createHorizontalBox();
			JLabel lableIcon=new JLabel();
			Icon icon=ResourseMannager.getImageIcon("disk.png", 32, 32);
			lableIcon.setIcon(icon);
			lableIcon.setText(file.getPath());
			box.add(lableIcon);
			JProgressBar progressBar=new JProgressBar();
			int value=0;
			long total=file.getTotalSpace()/1024/1024/1024;
			long free=file.getFreeSpace()/1024/1024/1024;
			long usable=total-free;
			
			if(total!=0){
				value=(int) (((usable*1.0)/total)*100);
			}
			progressBar.setValue(value);
			box.add(progressBar);
			JLabel totalLable=new JLabel("\t总空间:"+total+"G");
			totalLable.setForeground(new java.awt.Color(0, 204, 0));
			box.add(totalLable);
			JLabel freeLable=new JLabel("\t可用:"+free+"G");
			freeLable.setForeground(Color.red);
			box.add(freeLable);
			this.add(box,BorderLayout.WEST);
		}
		
	}
}